package com.qf.fmall2302.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.fmall2302.product.entity.ProductComments;
import com.qf.fmall2302.product.mapper.ProductCommentsMapper;
import com.qf.fmall2302.product.service.IProductCommentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fmall2302.user.entity.Users;
import com.qf.fmall2302.user.mapper.UsersMapper;
import com.qf.fmall2302.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品评价  服务实现类
 * </p>
 *
 * @author dongjl
 * @since 2023-08-23
 */
@Service
public class ProductCommentsServiceImpl extends ServiceImpl<ProductCommentsMapper, ProductComments> implements IProductCommentsService {

    @Autowired
    UsersMapper usersMapper;

    @Override
    public PageVo detailCommonts(String productid, Integer pageNum, Integer limit) {

        //1. 分页查询 ，mp分页插件如何使用，注意，现在配置类中把 分页插件配置打开
        Page<ProductComments> page = new Page<>(pageNum, limit);
        this.page(page, new QueryWrapper<ProductComments>().eq("product_id", productid));

        //2. 补充评论信息中缺少的用户名、昵称、用户图片信息
        addUserInfo(page.getRecords());

        PageVo pageVo = new PageVo();
        pageVo.setPageCount(limit);
        pageVo.setCount(page.getTotal());
        pageVo.setList(page.getRecords());

        return pageVo;
    }

    @Override
    public HashMap<String, Object> detailCommontscount(String productid) {

        //1. 查询好评条数
        long goodTotal = this.count(new QueryWrapper<ProductComments>().eq("product_id", productid).eq("comm_type", 1));

        //2. 查询中评条数
        long midTotal = this.count(new QueryWrapper<ProductComments>().eq("product_id", productid).eq("comm_type", 0));

        //3. 查询差评条数
        long badTotal = this.count(new QueryWrapper<ProductComments>().eq("product_id", productid).eq("comm_type", -1));

        //4. 总条数
        long total= goodTotal+midTotal+badTotal;

        //5. 计算好评率
        String percent = new BigDecimal(goodTotal)
                .multiply(new BigDecimal(100))
                .divide(new BigDecimal(total), 2, RoundingMode.HALF_UP)
                .toString();

        HashMap<String, Object> hashMap = new HashMap<>();

        hashMap.put("goodTotal",goodTotal);
        hashMap.put("total",total);
        hashMap.put("badTotal",badTotal);
        hashMap.put("midTotal",midTotal);
        hashMap.put("percent",percent);

        return hashMap;
    }

    @Override
    public HashMap<String, Object> detailCommontscountBysql(String productid) {

        /**
         * SELECT comm_type,COUNT(*) as count FROM product_comments
         * WHERE product_id = '3'
         * GROUP BY comm_type;
         */
        List<Map<String, Object>> listMaps = this.listMaps(new QueryWrapper<ProductComments>().select("comm_type,COUNT(*) as count")
                .eq("product_id", productid)
                .groupBy("comm_type"));

        long goodTotal = 0;
        long badTotal = 0;
        long midTotal =0;

        for (Map<String, Object> lineMap : listMaps) {

            Integer value = (Integer) lineMap.get("comm_type") ;
//            long l = value.longValue();
            if (value.equals(1)){
                // 好评
                goodTotal= (long) lineMap.get("count");
            }else if(value.equals(-1)){
                badTotal= (long) lineMap.get("count");
            }else if(value.equals(0)){
                midTotal= (long) lineMap.get("count");
            }

        }

        //4. 总条数
        long total= goodTotal+midTotal+badTotal;

        //5. 计算好评率
        String percent = new BigDecimal(goodTotal)
                .multiply(new BigDecimal(100))
                .divide(new BigDecimal(total), 2, RoundingMode.HALF_UP)
                .toString();

        HashMap<String, Object> hashMap = new HashMap<>();

        hashMap.put("goodTotal",goodTotal);
        hashMap.put("total",total);
        hashMap.put("badTotal",badTotal);
        hashMap.put("midTotal",midTotal);
        hashMap.put("percent",percent);

        return hashMap;
    }

    private void addUserInfo(List<ProductComments> comments) {

        for (ProductComments comment : comments) {
            String userId = comment.getUserId();
            Users user = usersMapper.selectById(userId);
            comment.setUsername(user.getUsername());
            comment.setNickname(user.getNickname());
            comment.setUserImg(user.getUserImg());
        }

    }
}
